package xiangmu

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
	"zhuandan/data_source"
	"zhuandan/models"
)

type JinzhanBody struct {
	ID        int64   `json:"id" gorm:"id"`
	Addtime   int64   `json:"addtime" gorm:"addtime"`
	Adminid   int64   `json:"adminid" gorm:"adminid"`
	Adminuser string  `json:"adminuser" gorm:"adminuser"`
	Title     string  `json:"title" gorm:"title"`
	Jindu     float64 `json:"jindu" gorm:"jindu"`
	State     int64   `json:"state" gorm:"state"` // 1目标 2kr 3事务
	Otherid   int64   `json:"otherid" gorm:"otherid"`
	Status    int64   `json:"status" gorm:"status"`
	Content   string  `json:"content" gorm:"content"`
	Name      string  `json:"name" gorm:"name"`
	Price     float64 `json:"price" gorm:"price"`
}

func Jinzhans(ctx *gin.Context) {
	request := JinzhanBody{}
	ctx.ShouldBind(&request)
	state := request.State
	otherid := request.Otherid

	var jinzhan []models.Xiangmujd
	data_source.Db.Where("state = ?", state).Where("otherid = ?", otherid).Order("id desc").Find(&jinzhan)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    jinzhan,
		"msg":     "ok",
	})
	return

}

func Jinzhanadd(ctx *gin.Context) {
	request := JinzhanBody{}
	ctx.ShouldBind(&request)
	title := request.Title
	adminid := request.Adminid
	adminuser := request.Adminuser
	jindu := request.Jindu
	state := request.State
	status := request.Status
	otherid := request.Otherid

	mb := models.Xiangmujd{
		Addtime:   time.Now().Unix(),
		Adminid:   adminid,
		Title:     title,
		Jindu:     jindu,
		State:     state,
		Otherid:   otherid,
		Adminuser: adminuser,
	}
	data_source.Db.Create(&mb)

	currentTime := time.Now()

	// 格式化时间为 yyyy-mm-dd 格式
	dateString := currentTime.Format("2006-01-02")

	if state == 1 {
		mb1 := models.Xiangmumb{
			Updatetime: time.Now().Unix(),
			Jindu:      jindu,
			Jinzhan:    title + "-" + dateString,
			Status:     status,
		}
		data_source.Db.Where("id = ?", otherid).Updates(&mb1)
	} else if state == 2 {
		mb1 := models.Xiangmukr{
			Updatetime: time.Now().Unix(),
			Jindu:      jindu,
			Jinzhan:    title + "-" + dateString,
			Status:     status,
		}
		data_source.Db.Where("id = ?", otherid).Updates(&mb1)
	} else if state == 3 {
		mb1 := models.Xiangmusw{
			Updatetime: time.Now().Unix(),
			Jinzhan:    title + "-" + dateString,
			Status:     status,
		}
		data_source.Db.Where("id = ?", otherid).Updates(&mb1)
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Jinzhandel(ctx *gin.Context) {
	request := JinzhanBody{}
	ctx.ShouldBind(&request)
	title := request.Title
	adminid := request.Adminid
	adminuser := request.Adminuser
	jindu := request.Jindu
	state := request.State
	status := request.Status
	otherid := request.Otherid

	mb := models.Xiangmujd{
		Addtime:   time.Now().Unix(),
		Adminid:   adminid,
		Title:     title,
		Jindu:     jindu,
		State:     state,
		Otherid:   otherid,
		Adminuser: adminuser,
	}
	data_source.Db.Create(&mb)

	currentTime := time.Now()

	// 格式化时间为 yyyy-mm-dd 格式
	dateString := currentTime.Format("2006-01-02")

	if state == 1 {
		mb1 := models.Xiangmumb{
			Updatetime: time.Now().Unix(),
			Jindu:      jindu,
			Jinzhan:    title + "-" + dateString,
			Status:     status,
		}
		data_source.Db.Where("id = ?", otherid).Updates(&mb1)
	} else if state == 2 {
		mb1 := models.Xiangmukr{
			Updatetime: time.Now().Unix(),
			Jindu:      jindu,
			Jinzhan:    title + "-" + dateString,
			Status:     status,
		}
		data_source.Db.Where("id = ?", otherid).Updates(&mb1)
	} else if state == 3 {
		mb1 := models.Xiangmusw{
			Updatetime: time.Now().Unix(),
			Jinzhan:    title + "-" + dateString,
			Status:     status,
		}
		data_source.Db.Where("id = ?", otherid).Updates(&mb1)
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
func Shejilist(ctx *gin.Context) {
	request := JinzhanBody{}
	ctx.ShouldBind(&request)
	state := request.State
	otherid := request.Otherid

	var jinzhan []models.Xiangmusj
	data_source.Db.Where("state = ?", state).Where("otherid = ?", otherid).Order("id desc").Find(&jinzhan)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    jinzhan,
		"msg":     "ok",
	})
	return

}

func Shejiinfo(ctx *gin.Context) {
	request := JinzhanBody{}
	ctx.ShouldBind(&request)
	state := request.State
	otherid := request.Otherid

	var jinzhan models.Xiangmusj
	data_source.Db.Where("state = ?", state).Where("otherid = ?", otherid).Last(&jinzhan)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    jinzhan,
		"msg":     "ok",
	})
	return

}

func Shejiadd(ctx *gin.Context) {
	request := JinzhanBody{}
	ctx.ShouldBind(&request)
	content := request.Content
	adminid := request.Adminid
	adminuser := request.Adminuser
	state := request.State
	otherid := request.Otherid
	name := request.Name
	price := request.Price

	mb := models.Xiangmusj{
		Addtime:   time.Now().Unix(),
		Adminid:   adminid,
		Content:   content,
		State:     state,
		Otherid:   otherid,
		Adminuser: adminuser,
		Name:      name,
		Price:     price,
	}
	data_source.Db.Create(&mb)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Shejiupdate(ctx *gin.Context) {
	request := JinzhanBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	content := request.Content
	adminid := request.Adminid
	adminuser := request.Adminuser
	state := request.State
	otherid := request.Otherid
	name := request.Name
	price := request.Price
	mb := models.Xiangmusj{
		Adminid:   adminid,
		Content:   content,
		State:     state,
		Otherid:   otherid,
		Adminuser: adminuser,
		Name:      name,
		Price:     price,
	}
	data_source.Db.Where("id = ?", id).Updates(&mb)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
